import requests
import pandas as pd
import pymysql
import json

db = pymysql.connect(host='localhost', port=23306, user='root', password='Pr0dkcd_', db="kcdoc")
cursor = db.cursor()
session = requests.Session()


def runJsonData(taskId, unqualifiedRemark,correctiveMeasure,comment):
    data = {
        "taskId": taskId,
        "seqFlow": "fl_gzdfq2gycs",
        "seqFlowName": None,
        "comment": comment,
        "params": {
            "unqualifiedFeatures": 2,
            "faultType": 3,
            "unqualifiedRemark": unqualifiedRemark,
            "correctiveMeasure": correctiveMeasure,
            "smallProblem": 0,
            "faceProblem": 0
        }
    }
    return data


def login():
    url = "http://127.0.0.1:22080/kcdoc/delegateLogin"
    payload = {'token': '170222e6947b7641ce8d3afc15df4d39'}
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    response = session.post(url, data=payload, headers=headers)
    data = response.text
    print(data)


def completeTask(runJson):
    url = "http://127.0.0.1:22080/kcdoc/activiti/kctask/completeTask"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    response = session.post(url, data=runJson, headers=headers)
    data = response.text
    print(data)


if __name__ == '__main__':
    login()
    data = pd.read_excel("C:\\Users\\asus\\Desktop\\导出交付不合格审理单0822.xlsx", sheet_name='Sheet1',
                         usecols=['问题单号', '处理意见'])
    for index, row in data.iterrows():
        runSql = """
         SELECT
                kqb.unqualified_remark,
                kqb.corrective_measure,
            task.ID_
            FROM
                kc_qc_base kqb
                LEFT JOIN act_ru_task task ON kqb.instanceId = task.PROC_INST_ID_
            WHERE
                kqb.form_num ='{0}'
       """.format(row['问题单号'])
        cursor.execute(runSql)
        rowInfo = cursor.fetchone()
        if rowInfo is not None:
            json_data = runJsonData(rowInfo[2], rowInfo[0], rowInfo[1], row['处理意见'])
            dumps = json.dumps(json_data)
            print(dumps)
            completeTask(dumps)
